{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 行向量、列向量\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31d85d2b-23f1-4ede-89a4-e1253b1c51bc",
   "metadata": {},
   "source": [
    "该代码使用`numpy`库在Python中创建和转置行向量与列向量，演示了向量在不同形态之间的转换过程。这种转换在线性代数中非常重要，特别是在矩阵运算和数据处理过程中。\n",
    "\n",
    "### 代码细节\n",
    "\n",
    "1. **行向量转置为列向量**：代码首先定义了一个行向量$a\\_\\text{row} = [1, 2, 3]$，并通过转置操作`a_row.T`将其转换为列向量$b$。转置后的列向量表示为：\n",
    "\n",
    "   $$\n",
    "   b = \\begin{bmatrix} 1 \\\\ 2 \\\\ 3 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "   这种转置操作将原来的单行多列矩阵转换为多行单列矩阵。\n",
    "\n",
    "2. **列向量转置为行向量**：代码接下来定义了一个列向量$b\\_\\text{col} = \\begin{bmatrix} 1 \\\\ 2 \\\\ 3 \\end{bmatrix}$，并通过转置操作`b_col.T`将其转换为行向量$a$。转置后的行向量表示为：\n",
    "\n",
    "   $$\n",
    "   a = \\begin{bmatrix} 1 & 2 & 3 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "   这种操作将原来的单列多行矩阵转换为多列单行矩阵。\n",
    "\n",
    "### 总结\n",
    "\n",
    "该代码展示了如何通过转置操作在行向量和列向量之间进行切换。行向量与列向量的转置本质上是对矩阵维度的调整，使得数据可以在不同的维度上操作和计算，这在矩阵乘法、线性变换和机器学习数据处理中尤为关键。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63c9154e-352b-4df9-a23a-469c79a7b9a3",
   "metadata": {},
   "source": [
    "## 转置行向量和列向量的操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0bc2bf33-2005-41cd-8ea8-58dae9ab463b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入numpy库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad927e09-9187-48cd-b1a1-166fd0820dc0",
   "metadata": {},
   "source": [
    "## 定义一个行向量并转置为列向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dc7139a2-dc2b-43ba-b718-1b65a2f8b41b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_row = np.array([[1, 2, 3]])  # 定义一个行向量\n",
    "a_row  # 输出行向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "64a21eba-175f-4e4d-b56f-dc743812a5a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a_row.T  # 将行向量转置为列向量\n",
    "b  # 输出列向量"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e79be6f-2149-4764-9dea-6c7de866e03c",
   "metadata": {},
   "source": [
    "## 定义一个列向量并转置为行向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ee76779b-e513-40c3-8877-496d76217159",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b_col = np.array([[1], [2], [3]])  # 定义一个列向量\n",
    "b_col  # 输出列向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d7980f2b-f0de-4ac5-ad7e-fe604fbcb55d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = b_col.T  # 将列向量转置为行向量\n",
    "a  # 输出行向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db5c8cbf-2da5-4e2f-91fc-76c97986a261",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
